rm(list = setdiff(ls(), lsf.str()))
#Load data 
election12<-haven::read_dta("Study 1/Original Data/hf12a_EN_1.0p.dta")
personality11<-haven::read_dta("Study 1/Original Data/cp11d_1.0p_EN.dta")
personality12<-haven::read_dta("Study 1/Original Data/cp12e_1.0p_EN.dta") # 

politics12<-haven::read_dta("Study 1/Original Data/cv12e_EN_1.0p.dta")
politics13<-haven::read_dta("Study 1/Original Data/cv13f_EN_1.0p.dta")
background12<-haven::read_dta("Study 1/Original Data/avars_201209_EN_1.0p.dta")

#merge data
data_1 <- merge(election12, personality12,by="nomem_encr", all=T)
data_2 <- merge(data_1, personality11,by="nomem_encr", all=T)
data_3 <- merge(data_2, politics12,by="nomem_encr", all=T)
data <- merge(data_3, background12,by="nomem_encr")

data <- data[,which(colnames(data)%in%c("nomem_encr", "hf12a004", "hf12a006", "oplcat", "nettocat", "geslacht", "leeftijd", "cv12e101", "cv12e104", "cv12e103", "cv12e047", "cv12e048", "cv12e049", "cp11d020", "cp11d021", "cp11d022", "cp11d023", "cp11d024", "cp11d025", "cp11d026", "cp11d027", "cp11d028", "cp11d029", "cp11d030", "cp11d031", "cp11d032", "cp11d033", "cp11d034", "cp11d035", "cp11d036", "cp11d037", "cp11d038", "cp11d039", "cp11d040", "cp11d041", "cp11d042", "cp11d043", "cp11d044", "cp11d045", "cp11d046", "cp11d047", "cp11d048", "cp11d049", "cp11d050", "cp11d051", "cp11d052", "cp11d053", "cp11d054", "cp11d055", "cp11d056", "cp11d057", "cp11d058", "cp11d059", "cp11d060", "cp11d061", "cp11d062", "cp11d063", "cp11d064", "cp11d065", "cp11d066", "cp11d067", "cp11d068", "cp11d069", "cp12e020", "cp12e021", "cp12e022", "cp12e023", "cp12e024", "cp12e025", "cp12e026", "cp12e027", "cp12e028", "cp12e029", "cp12e030", "cp12e031", "cp12e032", "cp12e033", "cp12e034", "cp12e035", "cp12e036", "cp12e037", "cp12e038", "cp12e039", "cp12e040", "cp12e041", "cp12e042", "cp12e043", "cp12e044", "cp12e045", "cp12e046", "cp12e047", "cp12e048", "cp12e049", "cp12e050", "cp12e051", "cp12e052", "cp12e053", "cp12e054", "cp12e055", "cp12e056", "cp12e057", "cp12e058", "cp12e059", "cp12e060", "cp12e061", "cp12e062", "cp12e063", "cp12e064", "cp12e065", "cp12e066", "cp12e067", "cp12e068", "cp12e069"))]

#Code variables ---------

#Voted?
data$didnotvote<-ifelse(data$hf12a004==2,1,0)

#PVV
data$populist<-ifelse(data$hf12a006==9, 1,0)

#Populist, oither, did not vote
data$populist_other_not<-data$populist
data$populist_other_not[data$didnotvote==1]=2

#multinomial: government opposition parties
data$vote_pvv_multinom_govopp<-car::recode(data$hf12a006, "1=2; 2=3; 3=2; 4=3; 5=3; 6=3; 7=3; 8=3; 9=1; 10=3; 11=3; 12=NA; 13=NA")
data$vote_pvv_multinom_govopp<-as.factor(data$vote_pvv_multinom_govopp)
levels(data$vote_pvv_multinom_govopp) <- c("PVV", "Government", "Opposition")
data$vote_pvv_multinom_govopp <- relevel(data$vote_pvv_multinom_govopp, ref = "PVV")

#Education
data$education<-data$oplcat

#Income
data$income<-zero1(car::recode(data$nettocat, "13=NA; 14=NA"))
data$income[is.na(data$income)==TRUE]=2
data$income_missing<-ifelse(data$income==2,1,0) 

#sex
data$female<-ifelse(data$geslacht==2,1,0)

#age
data$age<-data$leeftijd

#left-right
data$lr_placement11<-data$cv12e101
data$lr_placement11<-car::recode(data$lr_placement11, "999=12")
data$lr_placement_missing<-ifelse(data$lr_placement11==11, 1,0)

#anti-immigration
data$anti_immi11<-car::recode(data$cv12e104, "99=NA")

#economic conservatism
data$econ_cons11<-6-(car::recode(data$cv12e103, "99=NA"))

#cynicism
data$cyn1_11<-ifelse(data$cv12e047==2,1,0)
data$cyn2_11<-ifelse(data$cv12e048==2,1,0)
data$cyn3_11<-ifelse(data$cv12e049==2,1,0)
data$cynicism11 <- rowMeans(data.frame(data$cyn1_11,data$cyn2_11,data$cyn3_11),na.rm=T)

#Openness in 2011
data$y11_open1 <-data$cp11d024 # Have a rich vocabulary
data$y11_open2 <-data$cp11d034 # Have a vivid imagination
data$y11_open3 <-data$cp11d044 # Have excellent ideas
data$y11_open4 <-data$cp11d054 # Am quick to understand things
data$y11_open5 <-data$cp11d059 # Use difficult words
data$y11_open6 <-data$cp11d064 # Spend time reflecting on things
data$y11_open7 <-data$cp11d069 # Am full of ideas
#reversed coded items
data$y11_rec_open8 <-car::recode(as.numeric(data$cp11d029),"1=5; 2=4; 4=2; 5=1", as.numeric=T) #		Have difficulty understanding abstract ideas
data$y11_rec_open9 <-car::recode(as.numeric(data$cp11d039),"1=5; 2=4; 4=2; 5=1", as.numeric=T) #Am not interested in abstract ideas
data$y11_rec_open10 <-car::recode(as.numeric(data$cp11d049),"1=5; 2=4; 4=2; 5=1", as.numeric=T) # Do not have a good imagination.

#Openness in 2012
data$y12_open1 <-data$cp12e024
data$y12_open2 <-data$cp12e034 
data$y12_open3 <-data$cp12e044
data$y12_open4 <-data$cp12e054
data$y12_open5 <-data$cp12e059
data$y12_open6 <-data$cp12e064
data$y12_open7 <-data$cp12e069
#reversed coded items
data$y12_rec_open8 <-car::recode(as.numeric(data$cp12e029),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_open9 <-car::recode(as.numeric(data$cp12e039),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_open10 <-car::recode(as.numeric(data$cp12e049),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 11 and 12
data$open1 <- with(data,rowMeans(data.frame(y12_open1,y11_open1),na.rm=T))
data$open2 <- with(data,rowMeans(data.frame(y12_open2,y11_open2),na.rm=T))
data$open3 <- with(data,rowMeans(data.frame(y12_open3,y11_open3),na.rm=T))
data$open4 <- with(data,rowMeans(data.frame(y12_open4,y11_open4),na.rm=T))
data$open5 <- with(data,rowMeans(data.frame(y12_open5,y11_open5),na.rm=T))
data$open6 <- with(data,rowMeans(data.frame(y12_open6,y11_open6),na.rm=T))
data$open7 <- with(data,rowMeans(data.frame(y12_open7,y11_open7),na.rm=T))
data$open8 <- with(data,rowMeans(data.frame(y12_rec_open8,y11_rec_open8),na.rm=T))
data$open9 <- with(data,rowMeans(data.frame(y12_rec_open9,y11_rec_open9),na.rm=T))
data$open10 <- with(data,rowMeans(data.frame(y12_rec_open10,y11_rec_open10),na.rm=T))

#Conscientiousness in 2011
data$y11_con1 <- data$cp11d022 
data$y11_con2 <- data$cp11d032
data$y11_con3 <- data$cp11d042
data$y11_con4 <- data$cp11d052
data$y11_con5 <- data$cp11d062
data$y11_con6 <- data$cp11d067
#reverse coded items
data$y11_rec_con7 <-car::recode(as.numeric(data$cp11d027),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_con8 <-car::recode(as.numeric(data$cp11d037),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_con9 <-car::recode(as.numeric(data$cp11d047),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_con10 <-car::recode(as.numeric(data$cp11d057),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#Conscientiousness in 2012
data$y12_con1 <- data$cp12e022
data$y12_con2 <- data$cp12e032
data$y12_con3 <- data$cp12e042
data$y12_con4 <- data$cp12e052
data$y12_con5 <- data$cp12e062
data$y12_con6 <- data$cp12e067
#reverse coded items
data$y12_rec_con7 <-car::recode(as.numeric(data$cp12e027),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_con8 <-car::recode(as.numeric(data$cp12e037),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_con9 <-car::recode(as.numeric(data$cp12e047),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_con10 <-car::recode(as.numeric(data$cp12e057),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 11 and 12
data$con1 <- with(data,rowMeans(data.frame(y12_con1,y11_con1),na.rm=T))
data$con2 <- with(data,rowMeans(data.frame(y12_con2,y11_con2),na.rm=T))
data$con3 <- with(data,rowMeans(data.frame(y12_con3,y11_con3),na.rm=T))
data$con4 <- with(data,rowMeans(data.frame(y12_con4,y11_con4),na.rm=T))
data$con5 <- with(data,rowMeans(data.frame(y12_con5,y11_con5),na.rm=T))
data$con6 <- with(data,rowMeans(data.frame(y12_con6,y11_con6),na.rm=T))
data$con7 <- with(data,rowMeans(data.frame(y12_rec_con7,y11_rec_con7),na.rm=T))
data$con8 <- with(data,rowMeans(data.frame(y12_rec_con8,y11_rec_con8),na.rm=T))
data$con9 <- with(data,rowMeans(data.frame(y12_rec_con9,y11_rec_con9),na.rm=T))
data$con10 <- with(data,rowMeans(data.frame(y12_rec_con10,y11_rec_con10),na.rm=T))

#Extraversion in 2011
data$y11_ext1 <- data$cp11d020
data$y11_ext2 <- data$cp11d030
data$y11_ext3 <- data$cp11d040
data$y11_ext4 <- data$cp11d050
data$y11_ext5 <- data$cp11d060
#reversed coded items
data$y11_rec_ext6 <-car::recode(as.numeric(data$cp11d025),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_ext7 <-car::recode(as.numeric(data$cp11d035),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_ext8 <-car::recode(as.numeric(data$cp11d045),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_ext9 <-car::recode(as.numeric(data$cp11d055),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_ext10 <-car::recode(as.numeric(data$cp11d065),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#Extraversion in 2012
data$y12_ext1 <- data$cp12e020
data$y12_ext2 <- data$cp12e030
data$y12_ext3 <- data$cp12e040
data$y12_ext4 <- data$cp12e050
data$y12_ext5 <- data$cp12e060
#reversed coded items
data$y12_rec_ext6 <-car::recode(as.numeric(data$cp12e025),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_ext7 <-car::recode(as.numeric(data$cp12e035),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_ext8 <-car::recode(as.numeric(data$cp12e045),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_ext9 <-car::recode(as.numeric(data$cp12e055),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_ext10 <-car::recode(as.numeric(data$cp12e065),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 2011 & 2012
data$ext1 <- with(data,rowMeans(data.frame(y12_ext1,y11_ext1),na.rm=T))
data$ext2 <- with(data,rowMeans(data.frame(y12_ext2,y11_ext2),na.rm=T))
data$ext3 <- with(data,rowMeans(data.frame(y12_ext3,y11_ext3),na.rm=T))
data$ext4 <- with(data,rowMeans(data.frame(y12_ext4,y11_ext4),na.rm=T))
data$ext5 <- with(data,rowMeans(data.frame(y12_ext5,y11_ext5),na.rm=T))
data$ext6 <- with(data,rowMeans(data.frame(y12_rec_ext6,y11_rec_ext6),na.rm=T))
data$ext7 <- with(data,rowMeans(data.frame(y12_rec_ext7,y11_rec_ext7),na.rm=T))
data$ext8 <- with(data,rowMeans(data.frame(y12_rec_ext8,y11_rec_ext8),na.rm=T))
data$ext9 <- with(data,rowMeans(data.frame(y12_rec_ext9,y11_rec_ext9),na.rm=T))
data$ext10 <- with(data,rowMeans(data.frame(y12_rec_ext10,y11_rec_ext10),na.rm=T))

#Agreeableness in 2011
data$y11_agre1 <- data$cp11d026
data$y11_agre2 <- data$cp11d036
data$y11_agre3 <- data$cp11d046
data$y11_agre4 <- data$cp11d056
data$y11_agre5 <- data$cp11d061
data$y11_agre6 <- data$cp11d066
#reversed coded items
data$y11_rec_agre7 <-car::recode(as.numeric(data$cp11d021),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_agre8 <-car::recode(as.numeric(data$cp11d031),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_agre9 <-car::recode(as.numeric(data$cp11d041),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_agre10 <-car::recode(as.numeric(data$cp11d051),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#Agreeableness in 2012
data$y12_agre1 <- data$cp12e026
data$y12_agre2 <- data$cp12e036
data$y12_agre3 <- data$cp12e046
data$y12_agre4 <- data$cp12e056
data$y12_agre5 <- data$cp12e061
data$y12_agre6 <- data$cp12e066
#reversed coded items
data$y12_rec_agre7 <-car::recode(as.numeric(data$cp12e021),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_agre8 <-car::recode(as.numeric(data$cp12e031),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_agre9 <-car::recode(as.numeric(data$cp12e041),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_agre10 <-car::recode(as.numeric(data$cp12e051),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 2011 and 2012
data$agre1 <- with(data,rowMeans(data.frame(y12_agre1,y11_agre1),na.rm=T))
data$agre2 <- with(data,rowMeans(data.frame(y12_agre2,y11_agre2),na.rm=T))
data$agre3 <- with(data,rowMeans(data.frame(y12_agre3,y11_agre3),na.rm=T))
data$agre4 <- with(data,rowMeans(data.frame(y12_agre4,y11_agre4),na.rm=T))
data$agre5 <- with(data,rowMeans(data.frame(y12_agre5,y11_agre5),na.rm=T))
data$agre6 <- with(data,rowMeans(data.frame(y12_agre6,y11_agre6),na.rm=T))
data$agre7 <- with(data,rowMeans(data.frame(y12_rec_agre7,y11_rec_agre7),na.rm=T))
data$agre8 <- with(data,rowMeans(data.frame(y12_rec_agre8,y11_rec_agre8),na.rm=T))
data$agre9 <- with(data,rowMeans(data.frame(y12_rec_agre9,y11_rec_agre9),na.rm=T))
data$agre10 <- with(data,rowMeans(data.frame(y12_rec_agre10,y11_rec_agre10),na.rm=T))

#neuroticism in 2011
data$y11_neu1 <- data$cp11d023
data$y11_neu2 <- data$cp11d033
data$y11_neu3 <- data$cp11d043
data$y11_neu4 <- data$cp11d048
data$y11_neu5 <- data$cp11d053
data$y11_neu6 <- data$cp11d058
data$y11_neu7 <- data$cp11d063
data$y11_neu8 <- data$cp11d068
#reversed coded items
data$y11_rec_neu9 <-car::recode(as.numeric(data$cp11d028),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y11_rec_neu10 <-car::recode(as.numeric(data$cp11d038),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#neuroticism in 2012
data$y12_neu1 <- data$cp12e023
data$y12_neu2 <- data$cp12e033
data$y12_neu3 <- data$cp12e043
data$y12_neu4 <- data$cp12e048
data$y12_neu5 <- data$cp12e053
data$y12_neu6 <- data$cp12e058
data$y12_neu7 <- data$cp12e063
data$y12_neu8 <- data$cp12e068
#reversed coded items
data$y12_rec_neu9 <-car::recode(as.numeric(data$cp12e028),"1=5; 2=4; 4=2; 5=1", as.numeric=T)
data$y12_rec_neu10 <-car::recode(as.numeric(data$cp12e038),"1=5; 2=4; 4=2; 5=1", as.numeric=T)

#merge 2011 and 2012
data$neu1 <- with(data,rowMeans(data.frame(y12_neu1,y11_neu1),na.rm=T))
data$neu2 <- with(data,rowMeans(data.frame(y12_neu2,y11_neu2),na.rm=T))
data$neu3 <- with(data,rowMeans(data.frame(y12_neu3,y11_neu3),na.rm=T))
data$neu4 <- with(data,rowMeans(data.frame(y12_neu4,y11_neu4),na.rm=T))
data$neu5 <- with(data,rowMeans(data.frame(y12_neu5,y11_neu5),na.rm=T))
data$neu6 <- with(data,rowMeans(data.frame(y12_neu6,y11_neu6),na.rm=T))
data$neu7 <- with(data,rowMeans(data.frame(y12_neu7,y11_neu7),na.rm=T))
data$neu8 <- with(data,rowMeans(data.frame(y12_neu8,y11_neu8),na.rm=T))
data$neu9 <- with(data,rowMeans(data.frame(y12_rec_neu9,y11_rec_neu9),na.rm=T))
data$neu10 <- with(data,rowMeans(data.frame(y12_rec_neu10,y11_rec_neu10),na.rm=T))

########################
#
#
#Create scales of the IPIP FFM 50 item per trait
#
#
########################


########################################
#
#create IPIP scales: 50 items
#
########################################

#create Openness scale
data$open<-(rowMeans(data.frame(data$open1, data$open2, data$open3, data$open4, data$open5, data$open6, data$open7, data$open8, data$open9, data$open10), na.rm=T))

#create Conscientiousness scale
data$con<-(rowMeans(data.frame(data$con1, data$con2, data$con3, data$con4, data$con5, data$con6, data$con7, data$con8, data$con9, data$con10), na.rm=T))

#create Extraversion scale
data$ext<-(rowMeans(data.frame(data$ext1, data$ext2, data$ext3, data$ext4, data$ext5, data$ext6, data$ext7, data$ext8, data$ext9, data$ext10), na.rm=T))

#create Agreeableness scale
data$agre<-(rowMeans(data.frame(data$agre1, data$agre2, data$agre3, data$agre4, data$agre5, data$ext6, data$agre7, data$agre8, data$agre9, data$agre10), na.rm=T))

#create Neuroticism scale
data$neu<-(rowMeans(data.frame(data$neu1, data$neu2, data$neu3, data$neu4, data$neu5, data$neu6, data$neu7, data$neu8, data$neu9, data$neu10), na.rm=T))

# Create education dummies
data$edu1<-ifelse(data$education==1,1,0)
data$edu2<-ifelse(data$education==2,1,0)
data$edu3<-ifelse(data$education==3,1,0)
data$edu4<-ifelse(data$education==4,1,0)
data$edu5<-ifelse(data$education==5,1,0)
data$edu6<-ifelse(data$education==6,1,0)

save(data, file="Study 1/Altered Data/Study1_NL_12.RData")
